﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_SMS_Send]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_SMS_Send];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_SMS_Send]
    @DocID int,
    @FlowID int,
    @StepID int,
    @StaffIDs nvarchar(4000),
    @Content nvarchar(4000),
    @StendTime DATETIME,
    @RepeatTimes int,
    @RepeatPeriod int
AS

SET NOCOUNT ON
DECLARE @IDS nvarchar(4000)
DECLARE @ID      nvarchar(100)
DECLARE @Staff_ID int

SET @IDS = @StaffIDs

WHILE CHARINDEX(',',@IDS)>0
BEGIN
    SET @ID = SUBSTRING(@IDS,1,CHARINDEX(',',@IDS)-1)

    IF ISNUMERIC(@ID)>0
    BEGIN
        SET @Staff_ID = CONVERT(INT,@ID)

        INSERT INTO UDS_SMS_MobileMsgSendBuffer
            (Sender,RecipientMobileNo,Content,SendTime,RepeatTimes,RepeatPeriod)
            SELECT staff_name,mobile,@content,@StendTime,@RepeatTimes,@RepeatPeriod
                FROM     uds_staff 
                WHERE     staff_id = @staff_id
        INSERT INTO uds_flow_message 
            (msgid,doc_id,flow_id,step_id,staff_id)
            VALUES(IDENT_CURRENT('UDS_SMS_MobileMsgSendBuffer'),@docid,@flowid,@stepid,@staff_id)

    END
    SET @IDS = SUBSTRING(@IDS,CHARINDEX(',',@IDS)+1,LEN(@IDS))
END
SET NOCOUNT OFF